<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <span v-else class="avatar-uploader-icon">上传图片</span>
    </el-upload>
    <i class="el-icon-circle-close icon-position" ></i>
  </div>
</template>

<script>
var COS = require('cos-js-sdk-v5')
var cos = new COS({
  SecretId: 'AKIDgBQO5VeW7QSDhtp1wZw3uUmoY1DDwOZT',
  SecretKey: 'e29zZsuf9MIxLUNzLQHsCG3ZomUu3wYu'
})
export default {
  name: 'img',
  props: {
    index: {
      type: [Number, String],
      required: true
    },
    // 接收图片地址，数据回填
    picurl: {
      type: [String, Array],
      required: true,
      default: ''
    }
  },
  data () {
    return {
      imageUrl: ''// 图片地址
    }
  },
  watch: {
    picurl (newval, lod) {
      this.imageUrl = newval
    }
  },
  created () {
    this.pic()
  },
  methods: {
    // 图片回显
    pic () {
      this.imageUrl = this.picurl
    },
    upload (res) {
      if (res.file) {
        // 让进度条显示
        this.progressShow = true
        cos.putObject({
          Bucket: 'ylh-1306621534', // 存储桶的名称，必须
          Region: 'ap-beijing', // 存储桶所在地域，必须字段
          Key: res.file.name, // 文件名，必须
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => { // 进度条
            console.log(progressData)
            this.percentage = progressData.percent * 100
          }
        }, (err, data) => { // 上传成功的回调
          // console.log(err || data)
          if (err === null && data.statusCode === 200) {
            this.imageUrl = 'https:' + data.Location
            this.$emit('imgUrl', this.imageUrl, this.index)
          }
        })
      }
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }

  }
}
</script>

<style scoped>
div {
  display: inline-block;
  position: relative;
  width: 100px;
  height: 50px;
}
/* 上传图片的部分 */
.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 80px;
  text-align: center;
  margin-left: 20px;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 15px;
  color: #8c939d;
  width: 50px;
  height: 40px;
  line-height: 40px;
}
.el-upload {
 height: 100%;
 width: 100%;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
.icon-position{
  position: absolute;
  top: -5px;
  right: -5px;
  color: #8c939d;
}
img{
  width: 100px;
  height: 50px;
}
</style>
